package com.zd.commodity.productMng.dao.impl;

import java.sql.Timestamp;
import java.util.HashMap;

import javax.annotation.Resource;

import org.apache.struts2.ServletActionContext;
import org.hibernate.SessionFactory;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.MatchMode;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.springframework.stereotype.Repository;

import com.zd.commodity.dao.impl.BaseDao;
import com.zd.commodity.productMng.dao.IProductAttrDictDao;
import com.zd.commodity.productMng.entity.ProductAttr;
import com.zd.commodity.productMng.entity.ProductAttrDict;
import com.zd.commodity.userMng.entity.User;

@Repository("productAttrDictDao")
public class ProductAttrDictDaoImpl extends BaseDao<ProductAttrDict> implements IProductAttrDictDao {

	@Resource(name = "sessionFactory")
	public void setSuperSessionFactory(SessionFactory sessionFactory){
		super.setSessionFactory(sessionFactory);
	}
	
	@Override
	public DetachedCriteria getDetachedCriteria(ProductAttrDict t1,ProductAttrDict t2,Object params){
		DetachedCriteria dc=DetachedCriteria.forClass(ProductAttrDict.class).createAlias("productAttr", "pa");
		if(t1!=null){
			/*if(t1.getValue()!=null && !t1.getValue().equals("")){
				dc.add(Restrictions.like("value", t1.getValue(), MatchMode.ANYWHERE));
			}*/
/*			
			if(t1.getType()!=null && !t1.getType().equals("")){
				dc.add(Restrictions.eq("type", t1.getType()));
			}*/
			if(t1.getOriginalValue() != null && !"".equals(t1.getOriginalValue())){
				dc.add(Restrictions.like("originalValue", t1.getOriginalValue()));
			}
			if(t1.getStandardValue()!=null && !t1.getStandardValue().equals("")){
				dc.add(Restrictions.like("standardValue", t1.getStandardValue(), MatchMode.ANYWHERE));
			}
			if(t1.getShortValue()!=null && !"".equals(t1.getShortValue())){
				dc.add(Restrictions.like("shortValue", t1.getShortValue()));
			}
			
			if(t1.getProductAttr()!=null){
				if(t1.getProductAttr().getName()!=null && !t1.getProductAttr().getName().equals("")){
					dc.add(Restrictions.like("pa.name", t1.getProductAttr().getName(), MatchMode.ANYWHERE));
				}
				if(t1.getProductAttr().getId()!=null && !t1.getProductAttr().getId().equals("")){
					dc.add(Restrictions.eq("pa.id", t1.getProductAttr().getId()));
				}
			}
		}
		dc.addOrder(Order.desc("creationTime"));
		return dc;
	}
	
	@Override
	public void add(ProductAttrDict t){
		User user=(User)ServletActionContext.getRequest().getSession().getAttribute("user");
	    
		Timestamp time=new Timestamp(System.currentTimeMillis());
		
		t.setCreationTime(time);
		t.setCreator(user.getUsername());
		getHibernateTemplate().save(t);
	}
	
	@Override
	public void update(ProductAttrDict t){
		User user=(User)ServletActionContext.getRequest().getSession().getAttribute("user");
	    
		Timestamp time=new Timestamp(System.currentTimeMillis());
		
		t.setUpdateTime(time);
		t.setUpdater(user.getUsername());
		getHibernateTemplate().update(t);
	}

	@Override
	public void deleteAttrDictByAttrId(String attrId) {
		HashMap<String, String> hashMap = new HashMap<String,String>();
		hashMap.put("attrId", attrId);
		super.executeSql("deleteAttrDictByAttrId",hashMap);
		
	}
	
}
